package ch10;

public class Ch10_3 {
	public static int search(int[] seq, int key) {
		int l = 0;
		int r = seq.length - 1;
		while (l <= r) {
			int mid = (l + r) / 2;
			if (key == seq[mid]) {
				return mid;
			}
			if (seq[l] <= seq[mid]) {
				if (seq[l] <= key && key < seq[mid]) {
					r = mid - 1;
				} else {
					l = mid + 1;
				}
				continue;
			} else if (seq[mid] <= seq[r]) {
				if (seq[mid] < key && key <= seq[r]) {
					l = mid + 1;
				} else {
					r = mid - 1;
				}
				continue;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		int[] seq = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14};
//		int key = 14;
//		System.out.println(search(seq, key));
		for (int i = 0; i < seq.length; i++) {
			System.out.println(search(seq, seq[i]));
		}
	}
}
